<template>
  <el-input readonly v-model="queryParams.input" placeholder="请点击查找" class="input-with-select">
    <template #prepend>
      <el-button @click="show">查找</el-button>
    </template>
  </el-input>

  <!-- 字典搜索对话框 -->
  <el-dialog :title="props.dictName" v-model="visible" width="700px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据查询" prop="dictLabel">
        <el-input v-model="queryParams.dictLabel" placeholder="请输入" clearable style="width: 200px"
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table highlight-current-row @select="select1" @row-click="clickRow" ref="refTable"
                :data="DictDataList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="字典标签" align="center" prop="dictLabel"/>
        <el-table-column label="字典键值" align="center" prop="dictValue"/>
        <el-table-column label="其它1" align="center" prop="param1"/>
        <el-table-column label="其它2" align="center" prop="param2"/>
        <el-table-column label="其它3" align="center" prop="param3"/>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="getList"/>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelectd">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="DictSearch">
import {
  typeList
} from "@/api/system/dict/data";

const props = defineProps({
  // 字典名称
  dictName: {
    type: String,
    default: "请传入字典名称",
  },
  // 字典类别
  dictType: {
    type: String,
    default: "",
  },
  // 是否单选
  isSingle: {
    type: Boolean,
    default: true,
  },
  // 数据
  params: {
    type: Object,
    default: null,
  }
});

const {
  proxy
} = getCurrentInstance();

const DictDataList = ref([]);
const visible = ref(false);
const total = ref(0);
let dicts = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  dictName: undefined,
  dictType: undefined,
  params: {},
  input: undefined
});

// 显示弹框
function show() {
	debugger;
  queryParams.dictName = props.dictName;
  queryParams.dictType = props.dictType;
  queryParams.params = props.params;
  // Object.assign(queryParams.params,props.params);
  getList();
  visible.value = true;
}

function select1(selection,row){
  if (props.isSingle) {
    proxy.$refs["refTable"].clearSelection();
  }
  proxy.$refs["refTable"].toggleRowSelection(row,true);
}

// 查询表数据
function getList() {
  typeList(queryParams).then(res => {
    DictDataList.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

const emit = defineEmits(["selectd"]);

/** 选择操作 */
function handleSelectd() {
  dicts.value = proxy.$refs["refTable"].getSelectionRows();
  if (dicts.value.length == 0) {
    proxy.$modal.msgError("请选择数据，至少一行!!");
    return;
  }
  // 测试一下
  let tempdictValue = dicts.value.map(item => item.dictValue);
  console.log(tempdictValue);
  queryParams.input = dicts.value.map(item => item.dictLabel);
  visible.value = false;
  emit("selectd", dicts.value);
}

defineExpose({
  show,
});
</script>